package com.sgb.kjwl.viewmodel.packers;

import android.text.TextUtils;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.WorkerLevel;
import com.sgb.kjwl.model.entity.packers.EmploymentApplyEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.activity.packers.EmploymentApplyActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author liwei
 * @date 2020/11/10
 * description:劳务用工报名的viewModel
 */
public class EmploymentApplyModel extends BaseViewModel {

    private EmploymentApplyActivity activity;

    public EmploymentApplyModel(EmploymentApplyActivity activity) {
        this.activity = activity;
    }

    /**
     * 报名请求
     * @param entity 报名要求申请的信息所对应的对象
     */
    public void employmentApply(EmploymentApplyEntity entity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().employmentApply2(entity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.employmentApplySuccess();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 请求验证码
     * @param telephone 请求验证码的电话号码
     */
    public void getEmploymentCode(String telephone) {
        Map<String, Object> map = new HashMap<>();
        map.put("telephone", telephone);
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getEmploymentCode(telephone).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 服务团队-联动工种
     * @param no
     */
    public void getWorkType(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getLinkAgeWorkType().
                enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            for (PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>> entity : response.body().getData()) {
                                for (PickSelectEntity<List<PickSelectEntity>> entity1 : entity.getChildren()) {
                                    if (TextUtils.equals(entity1.getNo(), no)) {
                                        activity.showPickWorkTypePop(entity1.getChildren());
                                        return;
                                    }
                                }
                            }

                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    /**
     * 用工类型
     */
    public void getEmploymentType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getEmploymentType().enqueue(new Callback<BaseEntity<List<WorkerLevel>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<WorkerLevel>>> call, Response<BaseEntity<List<WorkerLevel>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<WorkerLevel> workYears = response.body().getData();
                    List<PickSelectEntity> data = new ArrayList<>();
                    if (workYears != null && workYears.size() > 0) {
                        for (WorkerLevel workerLevel : workYears) {
                            PickSelectEntity entity = new PickSelectEntity();
                            entity.setNo(String.valueOf(workerLevel.getType()));
                            entity.setProjectscale(workerLevel.getDesc());
                            data.add(entity);
                        }
                    }
                    data.remove(0);
                    activity.showPickPackerTypePop(data);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<WorkerLevel>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 所在地区
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if ("200".equals(response.body().getCode())) {
                    activity.showAddressPop(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }
}
